#include "stdafx.h"
#include "XmlDataReader.h"
#include "..\Lib_Base\xml\tinyxml.h"

XmlDataReader::XmlDataReader()
{
	m_iRowNum=m_iColumnNum=0;
}

bool XmlDataReader::ReadFromFile( const char * szFileFullPath )
{
	TiXmlDocument doc;
	if (!doc.LoadFile(szFileFullPath))
	{
	//	CLog::Instance()->LOG_ERROR( "XmlDataReader::ReadFromFile: can not find %s.\n",szFileFullPath);
		return false;
	}
	TiXmlElement* root = doc.FirstChildElement();
	if (root == NULL)
	{
	//	CLog::Instance()->LOG_ERROR( "XmlDataReader::ReadFromFile: %s has no data.\n",szFileFullPath);
		return false;
	}

	TiXmlElement* pRowElement=root->FirstChildElement();
	if(pRowElement==NULL)
	{
//		CLog::Instance()->LOG_ERROR( "XmlDataReader::ReadFromFile: %s has no data2.\n",szFileFullPath);
		return false;
	}

	do 
	{
		StringRow row;
		uint32 iColumnNum=0;

		TiXmlElement*  pCellElement=pRowElement->FirstChildElement();
		if(pCellElement==NULL) break;
		do
		{
			iColumnNum++;
			const char * pszTxt=pCellElement->GetText();
			if(pszTxt) 
				row.m_vCellData.push_back(pCellElement->GetText());
			else
				row.m_vCellData.push_back("");

			pCellElement=pCellElement->NextSiblingElement();
		}while(pCellElement!=NULL);

		if(m_iRowNum==0 || m_iColumnNum==iColumnNum)
		{
			m_iColumnNum=iColumnNum;
			this->m_vRows.push_back(row);
			this->m_iRowNum++;
		}
		else
		{
	//		CLog::Instance()->LOG_ERROR( "XmlDataReader::ReadFromFile: %s ColumnNum do not consist with previous at row %d,\n",szFileFullPath,this->m_iRowNum+1);
			return false;
		}
	
		pRowElement=pRowElement->NextSiblingElement();	
	} while (pRowElement!=NULL);

	return true;
}